/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.texture;

import com.android1.amarena2d.commons.Provider;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;

public class TextureDef {

    String key;
    String path;
    Provider<Pixmap> pixmapProvider;
    Files.FileType fileType;
    TextureOptions textureOptions;


    public TextureDef(String path, Files.FileType fileType, TextureOptions textureOptions) {
        this.path = path;
        this.fileType = fileType;
        this.textureOptions = textureOptions;
        this.key = createKey(this);
    }

    public TextureDef(Provider<Pixmap> pixmapProvider, TextureOptions textureOptions) {
        this.pixmapProvider = pixmapProvider;
        this.textureOptions = textureOptions;
        this.key = createKey(this);
    }

     public TextureDef(Pixmap pixmap, TextureOptions textureOptions) {
         this(new PixmapProvider<Pixmap>(pixmap),textureOptions);
     }

    public TextureDef(String path, Files.FileType fileType, Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, Texture.TextureWrap uWrap, Texture.TextureWrap vWrap) {
        this.path = path;
        this.fileType = fileType;
        this.textureOptions = new TextureOptions(magFilter, minFilter, uWrap, vWrap);
        this.key = createKey(this);
    }

    public static String createKey(String path, Files.FileType fileType, Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, Texture.TextureWrap uWrap, Texture.TextureWrap vWrap) {
        return new StringBuilder().append(path).append(fileType.ordinal()).append(minFilter.ordinal()).append(magFilter.ordinal()).append(uWrap.ordinal()).append(vWrap.ordinal()).toString();
    }

    public static String createKey(Provider<Pixmap> pixmap, Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, Texture.TextureWrap uWrap, Texture.TextureWrap vWrap) {
        return new StringBuilder().append(pixmap).append(minFilter.ordinal()).append(magFilter.ordinal()).append(uWrap.ordinal()).append(vWrap.ordinal()).toString();
    }

    public static String createKey(TextureDef def) {
        if (def.isPixmap())
            return createKey(def.pixmapProvider, def.textureOptions.minFilter, def.textureOptions.magFilter, def.textureOptions.uWrap, def.textureOptions.vWrap);
        else
            return createKey(def.path, def.fileType, def.textureOptions.minFilter, def.textureOptions.magFilter, def.textureOptions.uWrap, def.textureOptions.vWrap);
    }

    public String getKey() {
        return key;
    }

    public String getPath() {
        return path;
    }

    public Provider<Pixmap> getPixmap() {
        return pixmapProvider;
    }

    public TextureOptions getTextureOptions() {
        return textureOptions;
    }


    public Files.FileType getFileType() {
        return fileType;
    }

    public Texture.TextureFilter getMinFilter() {
        return textureOptions.minFilter;
    }

    public Texture.TextureFilter getMagFilter() {
        return textureOptions.magFilter;
    }

    public Texture.TextureWrap getUWrap() {
        return textureOptions.uWrap;
    }

    public Texture.TextureWrap getVWrap() {
        return textureOptions.vWrap;
    }

    public boolean isPixmap() {
        return pixmapProvider != null;
    }

    public boolean isPath() {
        return path != null && fileType != null && pixmapProvider == null;
    }

    public static class PixmapProvider<Pixmap> implements Provider<Pixmap> {

        private final Pixmap pixmap;

        public PixmapProvider(Pixmap pixmap) {
            this.pixmap = pixmap;
        }

        @Override
        public Pixmap get() {
            return pixmap;
        }
    }


    @Override
    public String toString() {
        return "TextureInfo{" +
                "key='" + key + '\'' +
                ", path='" + path + '\'' +
                ", fileType=" + fileType +
                ", minFilter=" + textureOptions.minFilter +
                ", magFilter=" + textureOptions.magFilter +
                ", uWrap=" + textureOptions.uWrap +
                ", vWrap=" + textureOptions.vWrap +
                '}';
    }
}
